<template>
  <div class="login-container">
    <div class="loginBox">
      <el-form
        ref="loginForm"
        :model="loginForm"
        :rules="loginRules"
        class="login-form"
        auto-complete="on"
        label-position="left"
      >

        <div class="title-container">
          <!-- <h3 class="title">{{$t('login.title')}}</h3> -->
          <div class="logoInfo clearfix">
            <em class="logo" />
            <span class="logoFontZ">黑马面面</span>
            <span>用户登录</span>
          </div>
          <!-- <lang-select class="set-language" /> -->
        </div>

        <el-form-item prop="username">
          <span class="svg-container svg-container_login">
            <svg-icon icon-class="user" />
          </span>
          <el-input
            ref="username"
            v-model="loginForm.username"
            placeholder="Username"
            name="username"
            type="text"
            tabindex="1"
            auto-complete="on"
          />
        </el-form-item>

        <el-form-item prop="password">
          <span class="svg-container">
            <svg-icon icon-class="password" />
          </span>
          <el-input
            :key="passwordType"
            ref="password"
            v-model="loginForm.password"
            :type="passwordType"
            placeholder="Password"
            name="password"
            tabindex="2"
            auto-complete="on"
            @keyup.enter.native="handleLogin"
          />
          <span class="show-pwd" @click="showPwd">
            <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
          </span>
        </el-form-item>

        <el-button
          :loading="loading"
          type="primary"
          style="width:100%;margin-bottom:30px;"
          @click.native.prevent="handleLogin"
        >Login</el-button>

        <!-- <div class="tips">
          <span style="margin-right:20px;">username: admin</span>
          <span> password: any</span>
        </div> -->

      </el-form>
    </div>
  </div>
</template>

<script>
import { validUsername } from '@/utils/validate'
import { mapActions } from 'vuex'
import { login } from '@/api/user'
import shajs from 'sha.js'
export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (validUsername(value)) {
        callback(new Error('请输入正确的用户名'))
      } else {
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能低于6位'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        username: 'root@admin.com',
        password: '123456'
      },
      loginRules: {
        username: [
          { required: true, trigger: 'blur', validator: validateUsername }
        ],
        password: [
          { required: true, trigger: 'blur', validator: validatePassword }
        ]
      },
      loading: false,
      passwordType: 'password'
      // redirect: undefined
    }
  },
  // watch: {
  //   $route: {
  //     handler: function(route) {
  //       this.redirect = route.query && route.query.redirect
  //     },
  //     immediate: true
  //   }
  // },
  methods: {
    ...mapActions(['user/login']), // 引入方法
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      // this.$nextTick(() => {
      //   this.$refs.password.focus()
      // })
    },
    handleLogin() {
      // 表单手动校验
      this.$refs.loginForm.validate(async valid => {
        if (valid) {
          this.loading = true
          await this.$store
            .dispatch('user/LoginByUsername', {
              username: this.loginForm.username,
              password: shajs('sha256')
                .update(this.loginForm.password)
                .digest('hex')
            })
            .then(() => {
              this.loading = false
              this.$router.push(this.redirect || '/')
            })
            .catch(() => {
              this.loading = false
            })
        } else {
          // console.log('error submit!!')

          return false
        }
      })
      // this.$refs.loginForm.validate(async isOK => {
      //   if (isOK) {
      //     try {
      //       this.loading = true
      //       // await login()
      //       await this['user/login'](this.loginForm)
      //       this.$router.push('/')
      //       // console.log('');
      //       this.$message.success('登录成功')
      //     } catch (error) {
      //       console.log(error)
      //     } finally {
      //       this.loading = false
      //       this.$message.warning('登录cuowu')
      //     }
      //     // await this.$store.dispatch('user/login', this.loginForm).then(() => {
      //     //   this.$router.push({ path: this.redirect || '/' })
      //     //   this.loading = false
      //     // }).catch(() => {
      //     //   this.loading = false
      //     // })
      //   } // else {
      //   //   // console.log('error submit!!')
      //   //   return false
      //   // }
      // })
    }
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
$bg: #2d3a4b;
$light_gray: #333;

/* reset element-ui css */
.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;
    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      &:-webkit-autofill {

        -webkit-text-fill-color: #fff !important;
      }
    }
  }
  .el-form-item {
    border: 1px solid #DCDFE6;
    background: #fff;
    border-radius: 5px;
    color: #454545;
  }
  .el-button--medium{
    height: 50px;
    line-height: 20px;
    font-size: 22px;
  }
}
</style>

<style rel="stylesheet/scss" lang="scss" scoped>
$bg: #2d3a4b;
$dark_gray: #889aa4;
$light_gray: #333;

.login-container {
  position: fixed;
  height: 100%;
  width: 100%;
  background-color: $bg;

  background: url('./assets/bg.jpg');
  -moz-background-size: 100% 100%;
  background-size: 100% 100%;
  background-repeat: no-repeat;
  .loginBox{
     height: 100%;
     width: 100%;
    background: url('./assets/logingBg.png') no-repeat 100% 100%;
    position: relative;
  }
  .login-form {
    position: absolute;
    left: 20%;
    top: 50%;
    width: 520px;
    padding: 35px 35px 15px 35px;
    margin: -200px 0 0 0;
    background:#f5f5f5;
  }
  .tips {
    font-size: 14px;
    // color: #fff;
    margin-bottom: 10px;
    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }
  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    &_login {
      font-size: 20px;
    }
  }
  .title-container {
    position: relative;
    .title {
      font-size: 26px;
      // font-weight: 400;
      color: $light_gray;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
    .set-language {
      // color: #fff;
      position: absolute;
      top: 5px;
      right: 0px;
    }
  }
  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
  .thirdparty-button {
    position: absolute;
    right: 35px;
    bottom: 28px;
  }
}
.logoInfo{
  padding-bottom:15px;
}
.logoInfo span{
  font-size: 22px;
  padding: 0 10px;
  float: left;
}
.logoInfo .logo{
  background: url(./assets/loginLogo.png) no-repeat;
  display:inline-block;
  width: 35px;
  height: 26px;
  float: left;
}
</style>
